package com.nexgo.payment.trans.download;

import android.support.annotation.NonNull;

import com.nexgo.data.entity.RecvData;
import com.nexgo.payment.aop.annotation.ManageTransCondition;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.trans.AbsOnlinePresenter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 状态上送操作业务
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年07月12日
 */
@ManageTransCondition
public class ResetGpsPresenter extends AbsOnlinePresenter implements DownloadTransContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(ResetGpsPresenter.class.getSimpleName());

    private DownloadTransContract.View mView;

    public ResetGpsPresenter(@NonNull DownloadTransContract.View view, @NonNull DataSource repository) {
        super(view, repository);
        mView = checkNotNull(view);
    }

    @Override
    protected TransData getTransData() {
        TransData transData = new TransData();
        transData.setTransType(TransType.RESET_GPS);
        return transData;
    }

    @Override
    protected void onFirstAction() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                postMainThread(() -> mView.showOnlineProc(mTransData, null));
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, 1000);//1秒后执行TimeTask的run方法

    }

    @Override
    protected void onTransSuccess(RecvData recvData) {
        //同时清除基站信息
        //删除文件
        File file= new File("/data/data/com.nexgo.jhyzp"+"/shared_prefs","base_info.xml");
        if(file.exists())
        {
            file.delete();
            mLog.debug("删除成功");
        }
        postMainThread(() -> mView.showTransSuccess());
    }
}
